#version 310 es
precision highp float;
#include "base.glsl"

layout(location = COLOR0) in vec4 v_color;
layout(location = TEXCOORD0) in vec2 v_texCoord;

layout(set = 1, binding = 0) uniform sampler2D u_tex0;

layout(std140, set = 0, binding = 1) uniform fs_ub {
    vec4  u_textColor;     // text color
    vec4  u_effectColor;   // effect color (rgb = color, a = intensity)
    float u_effectWidth;   // glow width in pixels
    int   u_labelPass;     // 0: text, 1: glow, 2: shadow
};

layout(location = SV_Target0) out vec4 FragColor;

void main()
{
    float dist = texture(u_tex0, v_texCoord).x;
    float smoothing = fwidth(dist);
    float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, dist);

    if (u_labelPass == 0) {
        // Text pass: solid core
        FragColor = v_color * vec4(u_textColor.rgb, u_textColor.a * alpha);
    }
    else if (u_labelPass == 1) {
        // Glow pass: soft halo around text
        // Use distance field falloff to create smooth glow
        // Map u_effectWidth (in "visual units") into SDF domain
        float pivot = clamp(0.5 + (1.0 - u_effectWidth), 0.0, 1.0);
        float mu = smoothstep(0.0, pivot, dist);
        vec4 color = u_effectColor * (1.0 - alpha) + u_textColor * alpha;
        FragColor = v_color * vec4(color.rgb, max(alpha, mu) * color.a);
    }
    else {
        // Shadow pass: simple fill
        FragColor = v_color * vec4(u_effectColor.rgb, u_effectColor.a * alpha);
    }
}
